set     more off
set     matsize 800
clear   all
global  dpath = "[Userpath]\data and code"

use "$dpath\data\firmdata.dta",clear
g conc=cod/wastewater
sort countycode year

by countycode: egen p67=pctile( conc ) if year<stationyear,p(66.7)
by countycode: egen p67_1=mean(p67) 
drop p67
rename p67_1 p67

by countycode: egen p33=pctile( conc ) if year<stationyear,p(33.3)
by countycode: egen p33_1=mean(p33) 
drop p33
rename p33_1 p33

********************************************************************************
**********************************High******************************************
********************************************************************************
preserve
drop if conc==.
drop if conc<p67

*1.County-level data

foreach var in output wastewater cod nh3n gas so2 dust {
	bysort countycode year: egen `var's = sum(`var')
	drop `var'
	rename `var's `var'
	gen l`var' = log(`var')
}
duplicates drop countycode year, force

gen llgdpC = log(lgdpC)
drop if llgdpC == .

gen llgdp = log(lgdp)
drop if llgdp == .

*2. Remove outliers
winsor2 lwastewater, replace cuts(0.5 99.5) trim
winsor2 lcod, replace cuts(0.5 99.5) trim
winsor2 lgas, replace cuts(0.5 99.5) trim
winsor2 lso2, replace cuts(0.5 99.5) trim
winsor2 ldust, replace cuts(0.5 99.5) trim

*3. Generate variables
xtset countycode year

gen manualupstream = 1 if manualyear != 33000
replace manualupstream = 0 if manualupstream == .

gen treated = 1 if autoupstream == 1 & year >= autoyear
replace treated = 0 if treated == .

gen treatedM = 1 if manualupstream == 1 & year >= manualyear
replace treatedM = 0 if treatedM == .

cd "$dpath\results"

reghdfe lwastewater treated , absorb(countycode citycode#year) cluster(citycode)
estimates store reg1
esttab reg1 using TableIV.rtf,replace b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table IV column 1) keep(treated)
restore

********************************************************************************
**********************************Medium****************************************
********************************************************************************
preserve
drop if conc==.
drop if conc<p33
drop if conc>=p67

*1.County-level data

foreach var in output wastewater cod nh3n gas so2 dust {
	bysort countycode year: egen `var's = sum(`var')
	drop `var'
	rename `var's `var'
	gen l`var' = log(`var')
}
duplicates drop countycode year, force

gen llgdpC = log(lgdpC)
drop if llgdpC == .

gen llgdp = log(lgdp)
drop if llgdp == .

*2. Remove outliers
winsor2 lwastewater, replace cuts(0.5 99.5) trim
winsor2 lcod, replace cuts(0.5 99.5) trim
winsor2 lgas, replace cuts(0.5 99.5) trim
winsor2 lso2, replace cuts(0.5 99.5) trim
winsor2 ldust, replace cuts(0.5 99.5) trim

*3. Generate variables
xtset countycode year

gen manualupstream = 1 if manualyear != 33000
replace manualupstream = 0 if manualupstream == .

gen treated = 1 if autoupstream == 1 & year >= autoyear
replace treated = 0 if treated == .

gen treatedM = 1 if manualupstream == 1 & year >= manualyear
replace treatedM = 0 if treatedM == .

cd "$dpath\results"

reghdfe lwastewater treated , absorb(countycode citycode#year) cluster(citycode)
estimates store reg1
esttab reg1 using TableIV.rtf,append b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table IV column 2) keep(treated)
restore

********************************************************************************
**********************************Low*******************************************
********************************************************************************

preserve
drop if conc==.
drop if conc>=p33

*1.County-level data

foreach var in output wastewater cod nh3n gas so2 dust {
	bysort countycode year: egen `var's = sum(`var')
	drop `var'
	rename `var's `var'
	gen l`var' = log(`var')
}
duplicates drop countycode year, force

gen llgdpC = log(lgdpC)
drop if llgdpC == .

gen llgdp = log(lgdp)
drop if llgdp == .

*2. Remove outliers
winsor2 lwastewater, replace cuts(0.5 99.5) trim
winsor2 lcod, replace cuts(0.5 99.5) trim
winsor2 lgas, replace cuts(0.5 99.5) trim
winsor2 lso2, replace cuts(0.5 99.5) trim
winsor2 ldust, replace cuts(0.5 99.5) trim

*3. Generate variables
xtset countycode year

gen manualupstream = 1 if manualyear != 33000
replace manualupstream = 0 if manualupstream == .

gen treated = 1 if autoupstream == 1 & year >= autoyear
replace treated = 0 if treated == .

gen treatedM = 1 if manualupstream == 1 & year >= manualyear
replace treatedM = 0 if treatedM == .

cd "$dpath\results"

reghdfe lwastewater treated , absorb(countycode citycode#year) cluster(citycode)
estimates store reg1
esttab reg1 using TableIV.rtf,append b(%9.3f) se scalar(N r2_a) star(* 0.1 **  0.05  *** 0.01) title(Table IV column 3) keep(treated)
restore